package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * Created by YDZ on 2019/11/20.
 */
public class CommonFilter {
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();

    //redis初始化数据
    @BeforeClass(description = "初始化redis数据,所有的用户信息都是正常的用户信息，并打印插入的用户信息")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-name.csv";
        String mothodname = "businessbehavior";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,0);
    }
    @Test(description = "测试20个用户为正常信息，正常结果返回所有userid；校验传入的结果中是否全部返回，code状态码是否为200，msg信息是否为success")
    public void check20NormalUserIdsRequest(){
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String msg = jsonPath.getString("msg");
        //校验每个参数
        List<String> diffUserid = new ArrayList<String>(cuIds);
        for (String userid: cuIds ) {
            for (Object aResurlt : resurlt) {
                if (Objects.equals(String.valueOf(aResurlt),String.valueOf(userid))) {
                    diffUserid.remove(userid);
                    break;
                }
            }
        }
        System.out.println(diffUserid);
        Reporter.log("有被过滤掉的数据，具体的userid:"+diffUserid);
        Assert.assertEquals(diffUserid.size(),0);
        Assert.assertEquals(resurlt.size(),cuIds.size());
        Assert.assertEquals(resurlt.size(),20);
        Assert.assertEquals(msg,"success");
    }
    @Test(description = "请求参数cuIds为空的情况下，接口返回结果code为100，msg为param cuIds must be not null，data为空")
    public void necessarilyParamNullRequest(){
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds",null);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"100");
    }
    @Test(description = "请求参数cuIds为一个用户切用户信息正常时，返回该用户本身的cuid，code值返回为0")
    public void checkOnlyOneCuserIdRequest(){
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds","50638177");
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf(50638177));
        Assert.assertEquals(code,"200");
    }
    @Test(description = "参数cuIds为两个用户切用户信息正常时，返回两个用户信息，返回数据的size为2，code为0")
    public void checkTwoCuserIdsRequest(){
        HashMap hashMap = new HashMap();
        String cuIds="[50638177,53095378]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),2);
        Assert.assertEquals(code,"200");
    }
    @Test(description = "参数cuIds为两个用户，用户没有基本信息。返回结果中的data应该为空")
    public void checkCuserIdsNullRequest(){
        HashMap hashMap = new HashMap();
        String cuIds="[503668177,530665378]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"200");
    }

    //删除数据
    @AfterClass(description = "删除出入掉的数据，避免影响其他测试")
    public void removeData(){
        operationRedis.removeRedis(cuIds);
    }
}
